<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import resize from '@/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'pie'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    },
    value: {
      type: Number,
      default: 0
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const that = this
      //   const minRadius = parseInt(that.width) > parseInt(that.height) ? parseInt(that.width) : parseInt(that.height)
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: `${that.value}%`,
          subtext: `${that.title}`,
          left: 'center',
          top: 'center', //top待调整
          textStyle: {
            color: '#fff',
            fontSize: 15,
            fontFamily: 'DINAlternate-Bold'
          },
          subtextStyle: {
            color: '#fff',
            fontSize: 8,
            fontFamily: 'PingFangSC-Regular',
            top: 'center'
          },
          itemGap: -4 //主副标题间距
        },
        series: [
          {
            name: 'pie1',
            type: 'pie',
            clockWise: true,
            radius: ['30px', '40px'],
            itemStyle: {
              normal: {
                label: {
                  show: false
                },
                labelLine: {
                  show: false
                }
              }
            },
            hoverAnimation: false,
            data: [
              {
                value: 80,
                name: 'completed',
                itemStyle: {
                  normal: {
                    borderWidth: 5,
                    borderColor: {
                      colorStops: [
                        {
                          offset: 0,
                          color: '#1d54f7' || '#00cefc' // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: '#68eaf9' || '#367bec' // 100% 处的颜色
                        }
                      ]
                    },
                    color: {
                      // 完成的圆环的颜色
                      colorStops: [
                        {
                          offset: 0,
                          color: '#1d54f7' || '#00cefc' // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: '#68eaf9' || '#367bec' // 100% 处的颜色
                        }
                      ]
                    },
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  }
                }
              },
              {
                name: 'gap',
                value: 100 - that.value,
                itemStyle: {
                  normal: {
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    },
                    color: 'rgba(0, 0, 0, 0)',
                    borderColor: 'rgba(0, 0, 0, 0)',
                    borderWidth: 0
                  }
                }
              }
            ]
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
div[class^='pie'] {
  position: relative;
  z-index: 999;
  box-sizing: border-box;
  padding: 3px;
}
</style>
